package com.workflow.service.impl;

import java.util.List;

import com.base.bean.Cardinfo;
import com.common.bean.Datadict;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.workflow.bean.Salescard;
import com.workflow.bean.Salesdeptdetail;
import com.workflow.service.SalesdeptdetailService;

public class SalesdeptdetailServiceImpl extends AbstractManager implements
		SalesdeptdetailService {

	public void addSalesdeptdetail(Salesdeptdetail salesdeptdetail) {
		String hql = "select count(*) from Salesdeptdetail sdd where sdd.salesdept.salesno = ?";
		long count = 0;
		count = (Long)this.getHibernateTemplate().find(hql,salesdeptdetail.getSalesdept().getSalesno()).get(0);
		count %= 1000L;
		
//		Long num = Long.parseLong(DateHelper.dateToStr(new Date(), "yyyyMMddHmsS"));
//		num *= 1000L;		
//		num += count;
		
		String salesno = salesdeptdetail.getSalesdept().getSalesno();
		
		String salesdetailno = "XSD-" + salesno.substring(salesno.length()-9, salesno.length()) + count;
		salesdeptdetail.setSalesdetailno(salesdetailno);
		
		this.getHibernateTemplate().save(salesdeptdetail);
	}

	@SuppressWarnings("unchecked")
	public List<Salesdeptdetail> findAllSalesdeptdetail() {
		String hql = "from Salesdeptdetail sdd";
		return this.getHibernateTemplate().find(hql);
	}

	public Salesdeptdetail findSalesdeptdetail(String salesdetailno) {
		return (Salesdeptdetail) this.getHibernateTemplate().load(Salesdeptdetail.class, salesdetailno);
	}

	public PagerModel findSalesdeptdetailForPager() {
		String hql = "from Salesdeptdetail sdd order by sdd.salesdept.salesno ,sdd.salesdetailno ";
		return searchPaginated(hql);
	}

	public PagerModel findSalesdeptdetailForPager(String salesno) {
		String hql = "from Salesdeptdetail sdd where sdd.salesdept.salesno = ? order by sdd.salesdetailno";
		return searchPaginated(hql,salesno);
	}

	public void removeSalesdeptdetail(String salesdetailno) {
		Salesdeptdetail salesdeptdetail = (Salesdeptdetail)this.getHibernateTemplate().load(Salesdeptdetail.class, salesdetailno);
		if(salesdeptdetail != null) {
			String hql = "from Salescard sc where sc.salesdetailno = ? order by sc.listid ";
			List<Salescard> salescardlist = this.getHibernateTemplate().find(hql,salesdetailno);
			for(Salescard salescard : salescardlist) {
				Cardinfo cardinfo = (Cardinfo)this.getHibernateTemplate().load(Cardinfo.class, salescard.getCardno());
				Datadict datadictByStatus = new Datadict();
//				置卡状态为库存 状态编号 76l
				datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_STORE);
				cardinfo.setDatadictByStatus(datadictByStatus);
				cardinfo.setBelongto("");//取消所属状态，临时
				this.getHibernateTemplate().update(cardinfo);
				this.getHibernateTemplate().delete(salescard);
			}
			this.getHibernateTemplate().delete(salesdeptdetail);
		}
	}

	public void updateSalesdeptdetail(Salesdeptdetail salesdeptdetail) {
		this.getHibernateTemplate().update(salesdeptdetail);
	}
}
